/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simple;

import algorithm.AlgorithmErrorException;
import algorithm.Encoder;
import algorithm.Genotype;
import algorithm.Individual;
import algorithm.ObjectiveFunction;
import algorithm.PopulationInitializer;

/**
 *
 * @author Pierre
 */
public class BitStringPopulationInitializer implements PopulationInitializer {

    private int populationSize;
    private int nBits;

    public BitStringPopulationInitializer(int populationSize, int nBits) {
        this.populationSize = populationSize;
        this.nBits = nBits;
    }
    
    public ListPopulation createInitialPopulation() {
        ListPopulation population = new ListPopulation();
        for (int i=0;i<populationSize;++i) {
            Individual individual = new DefaultIndividual(createAGenotype());
            population.add(individual);
        } 
        return population;
    }
    
    private Genotype createAGenotype() {
        short[] bits = new short[nBits];
        for (int i=0;i<nBits;++i) {
            bits[i] = (Math.random() > 0.5) ? (short)1 : (short)0;
        }
        return new BitStringGenotype(bits);
    }

    public int getSize() {
        return populationSize;
    }

}
